package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Systemdictionary;
import cn.wolfcode.domain.Systemdictionaryitem;
import cn.wolfcode.quest.JsonResult;
import cn.wolfcode.quest.SystemDictionaryItemQuery;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.service.ISystemdictionaryitemService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//字典明细
@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryService dictionaryService;
    @Autowired
    private ISystemdictionaryitemService service;

    @RequestMapping("/list")
    public String lists(Model model, @ModelAttribute("qo")SystemDictionaryItemQuery qo){

        PageInfo pageInfo=service.selectList(qo);
        model.addAttribute("pageResult",pageInfo);

        model.addAttribute("systemDictionaryItems",dictionaryService.selectAll());



        return "/systemDictionaryItem/list";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Systemdictionaryitem systemdictionary){

        if(systemdictionary.getSequence()==null){
            int seq=  service.selectMaxSequence(systemdictionary.getParent_id());
            systemdictionary.setSequence(seq+1);
        }

        if(systemdictionary.getId()!=null){
            service.updateByPrimaryKey(systemdictionary);
        }else {

            service.insert(systemdictionary);
        }

        return new JsonResult();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        service.deleteByPrimaryKey(id);
        return new JsonResult();
    }
}
